package AIRProject.filemanager
{
	import flash.events.Event;
	import flash.events.FileListEvent;
	import flash.events.IEventDispatcher;
	import flash.filesystem.File;
	
	/**
	 * ...
	 * @author FireKnight
	 */
	public class BrowseForDirectory
	{
		protected var openHandler:Function
		protected var title:String;
		protected var file:File;
		
		public function BrowseForDirectory(title:String, openHandler:Function)
		{
			this.title = title;
			this.openHandler = openHandler;
			this.file = new File();
			file.addEventListener(Event.SELECT, selectHandler);
			file.addEventListener(FileListEvent.SELECT_MULTIPLE, selectMultipleHandler);
			file.addEventListener(Event.CANCEL, cancelHandler);
			doOpen();
		}
		
		protected function selectMultipleHandler(event:FileListEvent):void
		{
			if (openHandler != null)
			{
				openHandler(event.files)
			}
			disponse();
		}
		
		protected function selectHandler(event:Event):void
		{
			if (file.exists && openHandler != null)
			{
				openHandler(file)
			}
			disponse();
		}
		
		protected function cancelHandler(event:Event):void
		{
			if (openHandler != null)
			{
				openHandler(null)
			}
			disponse();
		}
		
		protected function doOpen():void
		{
			file.browseForDirectory(title);
		}
		
		protected function disponse():void
		{
			file.removeEventListener(Event.SELECT, selectHandler);
			file.removeEventListener(FileListEvent.SELECT_MULTIPLE, selectMultipleHandler);
			file.removeEventListener(Event.CANCEL, cancelHandler);
		}
	
	
	
	}

}




